package com.xframe.domain.repository;

import com.xframe.domain.emnus.XDelFlag;
import com.xframe.domain.role.SysFullMenu;
import com.xframe.domain.role.SysMenu;
import com.xframe.domain.role.SysMenuButton;
import com.xframe.domain.role.SysMenuConfigs;
import com.xframe.xdal.core.constant.ErrorCode;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.ConditionValueCollection;
import com.xframe.xdal.core.model.DataAccessResult;
import com.xframe.xdal.core.model.IModel;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.tran.XTransaction;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单视图的数据库存储库
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-12-22
 */
@Log4j2
public class SysFullMenuRepository extends SysBaseRepository{

    @XTransaction
    @Override
    public Integer createNew(IModel model, ITableInfo tableInfo){
        try {

            SysFullMenu sysFullMenu = (SysFullMenu)model;
            if(sysFullMenu.getMenuType().equals("Nav") || sysFullMenu.getMenuType().equals("Fun") || sysFullMenu.getMenuType().equals("Link")){
                return sysMenuCreateNew((SysMenu) model);

            }
            if(sysFullMenu.getMenuType().equals("Button")){
                return sysMenuCreateButton(sysFullMenu);
            }
            return -1;
        } catch (DalFrameException e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 修改一个对象到数据库
     * @param model 被保存的对象
     * @param tableInfo 数据表结构
     *
     * @return
     */
    @XTransaction
    @Override
    public Integer update(IModel model, ITableInfo tableInfo) throws DalFrameException {
        SysFullMenu sysFullMenu = (SysFullMenu)model;
        if(sysFullMenu.getMenuType().equals("Nav") || sysFullMenu.getMenuType().equals("Fun") || sysFullMenu.getMenuType().equals("Link")){
            return sysMenuUpdate((SysMenu) model);
        }
        if(sysFullMenu.getMenuType().equals("Button")){
            return sysMenuCreateButton(sysFullMenu);
        }
        return -1;
    }

    /**
     * 删除一个对象
     * @param model 被保存的对象
     * @param tableInfo 数据表结构
     *
     * @return
     */
    @XTransaction
    @Override
    public Integer delete(IModel model, ITableInfo tableInfo) throws DalFrameException {
        SysFullMenu sysFullMenu = (SysFullMenu)model;
        if(sysFullMenu.getMenuType().equals("Nav") || sysFullMenu.getMenuType().equals("Fun") || sysFullMenu.getMenuType().equals("Link")){
            DataAccessResult<Integer> result = defDataBaseAccess.deleteOperate(sysFullMenu,
                    TableInfoBuilder.BuildTableInfoByAnnotation(SysMenu.class));
            if(!result.getSuccessFlag()){
                throw new DalFrameException(result.getCode(),result.getException().getMessage());
            }
            return result.getResult();
        }
        if(sysFullMenu.getMenuType().equals("Button")){
            DataAccessResult<Integer> result = defDataBaseAccess.deleteOperate(sysFullMenu,
                    TableInfoBuilder.BuildTableInfoByAnnotation(SysMenuButton.class));
            return result.getResult();
        }
        return -1;
    }

    private int sysMenuCreateButton(SysFullMenu sysFullMenu){
        try {
            ITableInfo tableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(SysMenuButton.class);
            List<SysMenuButton> menuButtonss = new ArrayList<>();
            for(String buttonCode : sysFullMenu.getBtnCodes()){
                SysMenuButton sysMenuButton = new SysMenuButton();
                sysMenuButton.setMenuId(sysFullMenu.getParentId());
                sysMenuButton.setButtonCode(buttonCode);
//                sysMenuButton.setCreateBy(sysFullMenu.getCreateBy());
//                sysMenuButton.setUpdateBy(sysFullMenu.getCreateBy());
                sysMenuButton.setDelFlag(XDelFlag.getDefFlag());
                menuButtonss.add(sysMenuButton);
            }
            ConditionValueCollection conditionValueCollection = new ConditionValueCollection();
            conditionValueCollection.add(new ConditionValue("menuId",sysFullMenu.getParentId()));
            Integer delCount = defDataBaseAccess.deleteMany(tableInfo,conditionValueCollection);
            log.info(delCount);
            Integer count = defDataBaseAccess.batchSave(tableInfo,menuButtonss);
            return count;
        } catch (DalFrameException e) {
            e.printStackTrace();
            throw e;
        }
    }

    private int sysMenuCreateNew(SysMenu model){
        try {
            ITableInfo tableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(SysMenu.class);
            if(defDataBaseAccess.isExists(model,tableInfo)){
                throw new DalFrameException(ErrorCode.INSERT_CONFLICT,"DataExist By Ak");
            }
            // 菜单添加
            DataAccessResult<Integer> result = defDataBaseAccess.createNewOperate(model, tableInfo);
            if(!result.getSuccessFlag()){
                throw new DalFrameException(result.getCode(),result.getException().getMessage());
            }
            List<SysMenuConfigs> sysMenuConfigs = model.getMenuConfigsList();
            if (sysMenuConfigs != null && sysMenuConfigs.size() > 0) {
                for (SysMenuConfigs menuConfig : sysMenuConfigs) {
                    menuConfig.setMenuId(model.getMenuId());
//                    menuConfig.setCreateBy(model.getCreateBy());
//                    menuConfig.setUpdateBy(model.getCreateBy());
                    menuConfig.setDelFlag(model.getDelFlag());
                }
                ConditionValueCollection conditionValueCollection = new ConditionValueCollection();
                conditionValueCollection.add(new ConditionValue("menuId",model.getMenuId()));
                Integer delCount = defDataBaseAccess.deleteMany(TableInfoBuilder.BuildTableInfoByAnnotation(SysMenuConfigs.class),conditionValueCollection);
                defDataBaseAccess.batchSave(TableInfoBuilder.BuildTableInfoByAnnotation(SysMenuConfigs.class), sysMenuConfigs);
            }
            return result.getResult();
        } catch (DalFrameException e) {
            e.printStackTrace();
            throw e;
        }
    }


    private int sysMenuUpdate(SysMenu model){
        try {
            ITableInfo tableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(SysMenu.class);

            DataAccessResult<Integer> result = defDataBaseAccess.updateOperate(model, tableInfo,
                    true);
            if (!result.getSuccessFlag()) {
                throw new DalFrameException(result.getCode(), result.getException().getMessage());
            }
            List<SysMenuConfigs> sysMenuConfigs = model.getMenuConfigsList();
            if (sysMenuConfigs != null && sysMenuConfigs.size() > 0) {
                for (SysMenuConfigs menuConfig : sysMenuConfigs) {
                    menuConfig.setMenuId(model.getMenuId());
                    menuConfig.setCreateBy(model.getCreateBy());
                    menuConfig.setUpdateBy(model.getCreateBy());
                    menuConfig.setDelFlag(model.getDelFlag());
                }

                ConditionValueCollection conditionValueCollection = new ConditionValueCollection();
                conditionValueCollection.add(new ConditionValue("menuId",model.getMenuId()));
                Integer delCount = defDataBaseAccess.deleteMany(TableInfoBuilder.BuildTableInfoByAnnotation(SysMenuConfigs.class),conditionValueCollection);
                defDataBaseAccess.batchSave(TableInfoBuilder.BuildTableInfoByAnnotation(SysMenuConfigs.class), sysMenuConfigs);
            }
            return result.getResult();
        } catch (DalFrameException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
